﻿using Microsoft.Azure;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;

namespace CloudStorageLight.Core
{
    public class SystemSettings
    {

        public static SystemSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = LoadFromSystem();
                }
                return _instance;
            }
            set { _instance = value; }
        }
        private static SystemSettings _instance;

        public string BlobStorage { get; set; }

        public string BlobContainerName { get; set; }

        public bool FullTextServerRole { get; set; }

        public bool UseRMS { get; set; }

        public string WorkFolder { get; set; }

        public bool MultiDomain { get; set; }

        public string DropBoxAppKey { get; set; }
        public string DropBoxAppSecret { get; set; }

        public string OneDriveClientId { get; set; }
        public string OneDriveClientSecret { get; set; }

        public string GoogleDriveClientId { get; set; }
        public string GoogleDriveClientSecret { get; set; }

        public string MicrosoftAccountClientId { get; set; }
        public string MicrosoftAccountClientSecret { get; set; }

        public bool UseMySmtpServer { get; set; }

        public string MailUser { get; set; }

        public string MailPassword { get; set; }

        public string FromMailAddress { get; set; }

        public int FullTextWorkerThreadCount { get; set; }

        public string BlobEncryptKey { get; set; }

        public string MfaCertFile { get; set; }

        public string ApplicationCookieName { get; set; }

        public bool UseRAMDirectory { get; set; }

        public bool CanCreateCloudAccountOnline { get; set; }

        public bool CanUseFileBlobAdapter { get; set; }

        public long? DefaultStorageLimitSize { get; set; }

        public string GeneralEncryptKey { get; set; }

        public long? MaxUploadSize { get; set; }
        public long? MaxOperatableSize { get; set; }

        public bool SingleClientAffinity { get; set; }

        public string IdaRealm { get; set; }
        public string IdaAdfs { get; set; }
        public string IdaTenantId { get; set; }

        public string AuthMode { get; set; }

        public bool DisableLogin { get { return !string.IsNullOrEmpty(AuthMode) && AuthMode != "UserPassword" ; } }

        public static SystemSettings LoadFromSystem()
        {
            var newObj = new SystemSettings();
            var multiDomain = GetKeyValue("MultiDomain");
            newObj.MultiDomain = multiDomain == null ? false : bool.Parse(multiDomain);
            newObj.BlobStorage = GetKeyValue("BlobStorage");
            newObj.BlobContainerName = GetKeyValue("BlobContainerName") ?? "files";

            newObj.MailUser = GetKeyValue("MailUser");
            newObj.MailPassword = GetKeyValue("MailPassword");
            newObj.FromMailAddress = GetKeyValue("FromMailAddress");

            newObj.DropBoxAppKey = GetKeyValue("DropBoxAppKey");
            newObj.DropBoxAppSecret = GetKeyValue("DropBoxAppSecret");

            newObj.OneDriveClientId = GetKeyValue("OneDriveClientId");
            newObj.OneDriveClientSecret = GetKeyValue("OneDriveClientSecret");

            newObj.GoogleDriveClientId = GetKeyValue("GoogleDriveClientId");
            newObj.GoogleDriveClientSecret = GetKeyValue("GoogleDriveClientSecret");

            newObj.MicrosoftAccountClientId = GetKeyValue("MicrosoftAccountClientId");
            newObj.MicrosoftAccountClientSecret = GetKeyValue("MicrosoftAccountClientSecret");

            newObj.IdaRealm = GetKeyValue("IdaRealm");
            newObj.IdaAdfs = GetKeyValue("IdaAdfs");
            newObj.MicrosoftAccountClientId = GetKeyValue("MicrosoftAccountClientId");
            newObj.MicrosoftAccountClientSecret = GetKeyValue("MicrosoftAccountClientSecret");
            newObj.IdaTenantId = GetKeyValue("IdaTenantId");
            newObj.AuthMode = GetKeyValue("AuthMode");

            var workbool = false;
            bool.TryParse(GetKeyValue("FullTextServerRole"), out workbool);
            newObj.FullTextServerRole = workbool;

            var workint = 5;
            int.TryParse(GetKeyValue("FullTextWorkerThreadCount"), out workint);
            newObj.FullTextWorkerThreadCount = workint;
            

            newObj.BlobEncryptKey = GetKeyValue("BlobEncryptKey");
            newObj.GeneralEncryptKey = GetKeyValue("GeneralEncryptKey") ?? "YwvqKyyUvR1Ty73PwynF3qsoiXCC7GK2";

            newObj.MfaCertFile = GetKeyValue("MfaCertFile");

            newObj.ApplicationCookieName = GetKeyValue("ApplicationCookieName");

            workbool = false;
            bool.TryParse(GetKeyValue("UseRAMDirectory"), out workbool);
            newObj.UseRAMDirectory = workbool;

            workbool = false;
            bool.TryParse(GetKeyValue("UseRMS"), out workbool);
            newObj.UseRMS = workbool;

            newObj.WorkFolder = GetKeyValue("WorkFolder");
            if (string.IsNullOrEmpty(newObj.WorkFolder)) newObj.WorkFolder= Path.GetTempPath();

            workbool = false;
            bool.TryParse(GetKeyValue("CanCreateCloudAccountOnline"), out workbool);
            newObj.CanCreateCloudAccountOnline = workbool;

            workbool = false;
            bool.TryParse(GetKeyValue("CanUseFileBlobAdapter"), out workbool);
            newObj.CanUseFileBlobAdapter = workbool;

            workbool = false;
            bool.TryParse(GetKeyValue("UseMySmtpServer"), out workbool);
            newObj.UseMySmtpServer = workbool;

            workbool = false;
            bool.TryParse(GetKeyValue("SingleClientAffinity"), out workbool);
            newObj.SingleClientAffinity = workbool;

            var workValue = GetKeyValue("DefaultStorageLimitSize");
            if (workValue != null)
            {
                long worklong;
                if (long.TryParse(workValue, out worklong))
                {
                    newObj.DefaultStorageLimitSize = worklong;
                }
            }

            workValue = GetKeyValue("MaxUploadSize");
            if (workValue != null)
            {
                long worklong;
                if (long.TryParse(workValue, out worklong))
                {
                    newObj.MaxUploadSize = worklong;
                }
            }

            workValue = GetKeyValue("MaxOperatableSize");
            if (workValue != null)
            {
                long worklong;
                if (long.TryParse(workValue, out worklong))
                {
                    newObj.MaxOperatableSize = worklong;
                }
            }

            return newObj;
        }


        private static string GetKeyValue(string key)
        {
            return CloudConfigurationManager.GetSetting(key);
        }

    }

}